package com.aaa.power.controller;

import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.entity.User;
import com.aaa.power.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户信息表(User)表控制层
 *
 * @author makejava
 * @since 2020-11-11 20:46:20
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @RequestMapping("deleteById")
    public boolean deleteById(Integer userId) {
        return this.userService.deleteById(userId);
    }

    /**
     * 通过主键删除中间表数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @RequestMapping("deleteUserRoleById")
    public int deleteUserRoleById(Integer userId) {
        return userService.deleteUserRoleById(userId);
    }
    /**
     * 新增用户
     * @param user
     * @return
     */
    @RequestMapping("insert")
    public int insert(@RequestBody User user){
//        for (Map map1:map) {
//            System.out.println(map1);
//        }
        return this.userService.insert(user);
    }
    /**
     * 修改用户
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @RequestMapping("update")
    public User update(@RequestBody User user) {
        return userService.update(user);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        if (id==null){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),
                this.userService.queryById(id));
    }
    /**
     * 分页加各种条件查询
     * @param pageNo
     * @param pageSize
     * @param user
     * @return
     */
    @RequestMapping("queryPageByParam")
    //@RequestMapping(value = "queryPageByParam",method = RequestMethod.GET)
    public Result queryPageByParam(Integer pageNo, Integer pageSize, User user) {
        return userService.queryPageByParam(pageNo,pageSize,user);
    }
    /**
     *查询用户id对应的角色id集合
     * @param userId
     * @return
     */
    @GetMapping("listRoleIdsByUserId")
    public Result listRoleIdsByUserId(Integer userId){
        return userService.listRoleIdsByUserId(userId);
    }
    /**
     * 添加用户角色关联
     * @param userId
     * @param roleIds
     * @return
     */
    @RequestMapping("addUserAndRole")
    public Object addUserAndRole(Integer userId,String roleIds){
        return userService.addUserAndRole(userId,roleIds);
    }


    @GetMapping("queryUserByUserName")
    public User queryUserByUserName(@RequestParam("userName") String username){
        return this.userService.queryUserByUserName(username);
    }
}